package uplift.project.model;

import android.util.Log;

public class VolunteerEvent {

	private String name;
	private String category;
	private String location;
	private String date;
	private String startTime;
	private String endTime;
	private String description;
	private boolean userCreated;
	private boolean joined;
	
	/*
	 * Categories (I didn't want to do enumerated types, too lazy)
	 * 
	 * - Elderly
	 * - Food
	 * - Nature
	 * - Animals
	 * - Misc
	 */
	
	public VolunteerEvent(String n, String c, String l, String d, String st, String et, String desc, boolean uc, boolean j) {
		name = n;
		category = c;
		location = l;
		date = d;
		startTime = st;
		endTime = et;
		description = desc;
		userCreated = uc;
		joined = j;
	}
	
	/**
	 * String reprensentation of this class.
	 */
	public String toString() {
		return name + "\n" + category + "\n" + location + "\n" + date + "\n" + startTime + "\n" + endTime;
	}
	
	// Setter methods
	
	public void setName(String n) {
		name = n;
	}
	
	public void setCategory(String c) {
		category = c;
	}
	
	public void setLocation(String l) {
		location = l;
	}
	
	/**
	 * Sets the date of an event. Date must be in the form "Month Day, Year"
	 * Example: May 14, 2012
	 * 
	 * @param d - date of the event
	 */
	public void setDate(String d) {
		date = d;
	}
	
	/**
	 * Sets the start time of an event. Time must be in the form "00:00_m"
	 * Example: 5:00pm, 9:30am, 11:50pm.
	 * 
	 * @param st - start time of the event
	 */
	public void setStartTime(String st) {
		startTime = st;
	}
	
	/**
	 * Sets the end time of an event. Time must be in the form "00:00_m"
	 * Example: 5:00pm, 9:30am, 11:50pm.
	 * 
	 * @param et - end time of the event
	 */
	public void setEndTime(String et) {
		endTime = et;
	}
	
	public void setDescription(String desc) {
		description = desc;
	}
	
	public void join() {
		joined = true;
	}
	
	public void unregister() {
		joined = false;
	}
	
	// Getter methods
	
	public String getName(){
		return name;
	}
	
	public String getCategory(){
		return category;
	}
	
	public String getLocation(){
		return location;
	}
	
	/**
	 * Returns a String representing the date in the form "Month Day, Year"
	 * Example: May 14, 2012
	 * 
	 * @return Returns a String representing the date in the form "Month Day, Year"
	 */
	public String getDate(){
		return date;
	}
	
	/**
	 * Returns a String representing the date in the form MM-DD-YYYY
	 * Example: May 14, 2012 -> 5-14-2012
	 * 
	 * @return Returns a String representing the date in the form MM-DD-YYYY
	 */
	public String getNumericDate(){
		String[] parsed = date.split(" ");
		StringBuilder sb = new StringBuilder();
		
		sb.append(getMonthNumber(parsed[0]));
		sb.append("-");
		sb.append(parsed[1].substring(0, parsed[1].length() - 1));
		sb.append("-");
		sb.append(parsed[2]);
		
		return sb.toString();
	}
	
	/**
	 * Returns a String representing the start time of an event.
	 * Examples: 5:00pm, 9:30am
	 * 
	 * @return Returns a String representing the start time of an event.
	 */
	public String getStartTime(){
		return startTime;
	}
	
	/**
	 * Returns a String representing the end time of an event.
	 * Examples: 5:00pm, 9:30am
	 * 
	 * @return Returns a String representing the end time of an event.
	 */
	public String getEndTime(){
		return endTime;
	}
	
	public String getDescription(){
		return description;
	}
	
	public boolean isUserCreated() {
		return userCreated;
	}
	
	public boolean joined(){
		return joined;
	}
	
	
    private String getMonthNumber(String m) {
    	if (m.equals("January")) {
    		return "1";
    	} else	if (m.equals("February")) {
    		return "2";
    	} else	if (m.equals("March")) {
    		return "3";
    	} else	if (m.equals("April")) {
    		return "4";
    	} else	if (m.equals("May")) {
    		return "5";
    	} else	if (m.equals("June")) {
    		return "6";
    	} else	if (m.equals("July")) {
    		return "7";
    	} else	if (m.equals("August")) {
    		return "8";
    	} else	if (m.equals("September")) {
    		return "9";
    	} else	if (m.equals("October")) {
    		return "10";
    	} else	if (m.equals("November")) {
    		return "11";
    	} else	if (m.equals("December")) {
    		return "12";
    	} else {
    		// Should never actually reach here.
    		return "-1";
    	}
    }
    
    /**
     * Returns whether an event is equal to this one based on fields
     */
    public boolean isEqual(String n, String c, String l, String d, String st, String et, String desc) {
		return (this.getName().equalsIgnoreCase(n)) &&
				(this.getCategory().equalsIgnoreCase(c)) &&
				(this.getLocation().equalsIgnoreCase(l)) &&
				(this.getDate().equalsIgnoreCase(d)) &&
				(this.getStartTime().equals(st)) &&
				(this.getEndTime().equals(et)) &&
				(this.getDescription().equalsIgnoreCase(desc));
    }
	
    
    // ***************************NEED TO FIX THIS METHOD***************************
    /**
     * Returns whether an event matches the requirements
     * @param c category
     * @param l location
     * @param d date
     * @param st start time
     * @param et end time
     * @return
     */
    public boolean matches(String c, String l, String d, String st, String et) {
    	boolean result = true;
    	
    	if (!c.isEmpty()) {
    		if (!this.getCategory().equals(c)) { result = false; }
    	}	
    	
    	if (!l.isEmpty()) {
    		String regex = "(?i).*" + l.toLowerCase() + ".*";
    		if (!this.getLocation().matches(regex)) { result = false; }
    	}

    	if (!d.isEmpty()) {
    	    if (!this.getDate().equals(d)) { result = false; }
    	}

    	if (!st.isEmpty()) { 
    	    if (compareTimes(this.getStartTime(), st) < 0) {
    	    	result = false;
    	    }
    	}

    	if (!et.isEmpty()) {
    	    if (compareTimes(this.getEndTime(), et) > 0) {
    	    	result = false;
    	    }
    	}

    	return result;
    }
	
    /*
     * Returns 0 if the times are equal
     * Returns negative number if t1 comes before t2
     * Returns positive number if t1 comes after t2
     */
    private int compareTimes(String t1, String t2) {
    	int[] numericT1 = convertToTimeArray(t1);
    	int[] numericT2 = convertToTimeArray(t2);
    	
    	if (numericT1[0] == numericT2[0]) {
    		return numericT1[1] - numericT2[1];
    	} else {
    		return numericT1[0] - numericT2[0];
    	}
    }
    
    public int[] convertToTimeArray(String time) {
    	String meridiem = time.substring(time.length() - 2, time.length());
    	String[] splitTime = time.substring(0, time.length() - 2).split(":");
    	int[] numTime = new int[] { Integer.parseInt(splitTime[0]), Integer.parseInt(splitTime[1])};
    	
    	if (numTime[0] == 12 && meridiem.equals("am")) {
    		numTime[0] = 0;
    	}
    	
    	if (meridiem.equals("pm")) {
    		numTime[0] += 12;
    	}
    	
    	return numTime;
    }
	
}
